const menu = ["gpt-3.5-turbo",
     "gpt-3.5-turbo-0613", 
     "gpt-4", "gpt-4-0613", 
     "gpt-4o", "glm-4", 
     "moonshot-v1-8k", 
     "moonshot-v1-32k", 
     "moonshot-v1-128k", "phi3", 
     "mistral", 
     "qwen2.5",
     "yi",
     "llama3.2",
     "deepseek-v2",
     "deepseek-chat",
    "deepseek-reasoner"
    ];
//% color="#ef784c" iconWidth=50 iconHeight=40
namespace OpenAI{

    //% block="初始化模块，API密钥[API] base_url[URL]" blockType="command"
    //% API.shadow="string"   API.defl="xxx" 
    //% URL.shadow="dropdownRound"   URL.options="URL" 
    export function OpenAI_Iint1(parameter: any, block: any) {
        let api=parameter.API.code
        let url=parameter.URL.code
        Generator.addImport("from openai import OpenAI ")
        if (url.includes("http")) {
            Generator.addCode(`client = OpenAI(api_key=${api}, base_url="${url}")`)
        } else {
            Generator.addCode(`client = OpenAI(api_key=${api}, base_url=${url})`)
        }
        
    }
        //% block="基础调用" blockType="tag"
        export function tagtest() {}
        //% block="选择模型[MODEL]  temperature:[temperature]范围：(0.0,1.0] " blockType="command"
    //% MODEL.shadow="dropdownRound"   MODEL.options="MODEL" 
    
    //% temperature.shadow="number"   temperature.defl="0.9" 
    export function OpenAI_Iint(parameter: any, block: any) {
 
        let model=parameter.MODEL.code
    
        let temperature=parameter.temperature.code
        console.log(typeof(model))
        if (menu.includes(model)) {
            Generator.addCode(`
def askChat(question):
    res = client.chat.completions.create(
        model='${model}',
        temperature=${temperature},
        messages=[{"role": "user","content":question}]
    )
    message = res.choices[0].message.content
    return message`)
        } else {
            Generator.addCode(`
def askChat(question):
    res = client.chat.completions.create(
        model=${model},
        temperature=${temperature},
        messages=[{"role": "user","content":question}]
    )
    message = res.choices[0].message.content
    return message`)
        }

    }
    
    //% block="输入[MESS]返回结果" blockType="reporter"
    //% MESS.shadow="string"   MESS.defl="你好，AI机器人" 

    export function OpenAI_Iintsetvolume(parameter: any, block: any) {
        let mess=parameter.MESS.code
 
        Generator.addCode(`askChat(${mess})`)
        
    } 

    //% block="高级调用" blockType="tag"
    export function tagtest1() {}
    //% block="设置系统消息[MESS]temperature:[temperature]范围：(0.0,1.0] " blockType="command"
    //% MESS.shadow="string"   MESS.defl="你是一个有帮助的助手。"
    //% temperature.shadow="number"   temperature.defl="0.9" 
    export function OpenAI_Iint3(parameter: any, block: any) {

        let mess=parameter.MESS.code
        let temperature=parameter.temperature.code
        Generator.addCode(`system_message = ${mess}
def askChat(question,model):
    messages = [
        {"role": "system", "content": system_message},
        {"role": "user", "content": question}
    ]
    res = client.chat.completions.create(
        model=model,
        temperature=${temperature},
        messages=[{"role": "user","content":question}]
    )
    message = res.choices[0].message.content
    return message
`)
    
        
    }

        
    
    //% block="选择[MODEL]模型输入[MESS]返回结果" blockType="reporter"
    //% MODEL.shadow="dropdownRound"   MODEL.options="MODEL"
    //% MESS.shadow="string"   MESS.defl="你好，AI机器人" 

    export function OpenAI_Iintsetvolume1(parameter: any, block: any) {
        let model=parameter.MODEL.code
        let mess=parameter.MESS.code
        if (menu.includes(model)) {
            Generator.addCode(`askChat(${mess},'${model}')`)
        } else {
            Generator.addCode(`askChat(${mess},${model})`)
        }
     
        
    } 
   //% block="---"
   export function noteSep2() {
}

    //% block="将[VOICE]音频文件翻译成文本（OpenAI）" blockType="command"
    //% VOICE.shadow="string"   VOICE.defl="record.wav" 

    export function OpenAI_Iintvoice(parameter: any, block: any) {
        let voice=parameter.VOICE.code
 
        Generator.addCode(`
file1 = open(${voice}, "rb")
transcription = openai.Audio.transcribe("whisper-1",file1)`)
        
    } 
    //% block="返回翻译的结果（OpenAI）" blockType="reporter"

    export function OpenAI_Iintvoice_valve(parameter: any, block: any) {
 
        Generator.addCode(`transcription["text"]`)
        
    } 
}
